
from condition import CompoundCondition
from Brain.vector import Vector

class MovementStep:
	"""  """
	def __init__(self, x, y):
		self._destination = Vector(x, y)
	def copy(self):
		return MovementStep(self._destination.x, self._destination.y)

class StepWithTimeConstraint:
	DESCRIPTION = '!'
	def __init__(self, timelimit):
		self.timelimit = timelimit
	def copy(self):
		return self.__class__(self.timelimit)
	def __str__(self):
		return '%s [%s]' % (self.DESCRIPTION, self.timelimit, )
	
class BuildStep(StepWithTimeConstraint):
	DESCRIPTION = 'build'
class PlacelintelStep(StepWithTimeConstraint):
	DESCRIPTION = 'place lintel'
class FindpatternStep(StepWithTimeConstraint):
	DESCRIPTION = 'find pattern'
class ChasepakStep(StepWithTimeConstraint):
	DESCRIPTION = 'chase pack'

class ApproachsiteStep(StepWithTimeConstraint):
	def __init__(self, site, timelimit):
		StepWithTimeConstraint.__init__(self, timelimit)
		self.site = site
	def copy(self):
		return ApproachsiteStep(self.site, self.timelimit)
	def __str__(self):
		return 'goto site %s [%s]' % (self.site, self.timelimit, )

class Plan:
	""" 
	"""
	def __init__(self, name=''):
		self._start_condition = CompoundCondition()
		self._operations = []
		self._name = name
		self.timelimit = 15.0	## <<-- new attribute !!
		self.timelimit_bottom = 1.0	## <<-- new attribute !!

	def get_step(self, index):
		if index < 0 or index >= len(self._operations):
			return None
		return self._operations[index]
	
	def calculate_ranking(self, world):
		return self._start_condition.evaluate(world)
	
	def move_path(self):
		""" generate list of coordinates on path to traverse
		"""
		ret = []
		for op in self._operations:
			if isinstance(op, MovementStep):
				ret.append( op._destination.copy() )
			else:
				ret.append( op.copy() )
		return ret
	
	def copy(self):
		ret = Plan(self._name)
		ret._start_condition = self._start_condition.copy()
		for op in self._operations:
			ret._operations.append( op.copy() )

	## Planer part
	def add_movement_step(self, x, y):
		self._operations.append( MovementStep(x, y) )
	
	def add_build_step(self, timelimit):
		self._operations.append( BuildStep(timelimit) )

	def add_placelintel_step(self, timelimit):
		self._operations.append( PlacelintelStep(timelimit) )

	def add_approachsite_step(self, site, timelimit):
		self._operations.append( ApproachsiteStep(site, timelimit) )

	def add_findpattern_step(self, timelimit):
		self._operations.append( FindpatternStep(timelimit) )

	def add_chasepak_step(self, timelimit):
		self._operations.append( ChasepakStep(timelimit) )

	def remove_last_step(self):
		if len(self._operations) > 0:
			del self._operations[-1]
	
	def __str__(self):
		return '< %s : steps: %d limit: %s >' % (self._name, len(self._operations), self.timelimit, )







